<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
			<el-form-item label="活动主题" prop="subject">
				<el-input v-model="queryParams.subject" placeholder="请输入" clearable @keyup.enter="handleQuery"/>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择" clearable>
					<el-option v-for="dict in sys_data_status" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['act:actActivityInfo:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['act:actActivityInfo:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['act:actActivityInfo:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['act:actActivityInfo:export']">导出</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="actActivityInfoList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center"/>
			<el-table-column label="活动主题" align="center" prop="subject" v-if="columnVisible('subject')"/>
			<el-table-column label="活动详情" align="center" prop="details" v-if="columnVisible('details')"/>
			<el-table-column label="宣传图" align="center" prop="image" width="100" v-if="columnVisible('image')">
				<template #default="scope">
					<image-preview :src="scope.row.image" :width="50" :height="50"/>
				</template>
			</el-table-column>
			<el-table-column label="开始日期" align="center" prop="startDate" v-if="columnVisible('startDate')"/>
			<el-table-column label="结束日期" align="center" prop="endDate" v-if="columnVisible('endDate')"/>
			<el-table-column label="开始时间" align="center" prop="startTime" v-if="columnVisible('startTime')"/>
			<el-table-column label="结束时间" align="center" prop="endTime" v-if="columnVisible('endTime')"/>
			<el-table-column label="参与人数" align="center" prop="participantsNumber" v-if="columnVisible('participantsNumber')"/>
			<el-table-column label="活动地点" align="center" prop="activityAddr" v-if="columnVisible('activityAddr')"/>
			<el-table-column label="状态" align="center" prop="status" v-if="columnVisible('status')">
				<template #default="scope">
					<el-switch v-model="scope.row.status" :active-value="0" :inactive-value="1" @change="handleStatusChange(scope.row,'status')"></el-switch>
				</template>
			</el-table-column>
			<el-table-column label="归属部门" align="center" prop="deptId" v-if="columnVisible('deptId')">
				<template #default="scope">
					<span>{{ deptName(scope.row.deptId) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="备注" align="center" prop="remark" v-if="columnVisible('remark')"/>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
				<template #default="scope">
					<el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['act:actActivityInfo:query']" v-noPermi="['act:actActivityInfo:edit']">查看</el-button>
					<el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['act:actActivityInfo:edit']">修改</el-button>
					<el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['act:actActivityInfo:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList"/>

		<!-- 添加或修改活动管理对话框 -->
		<el-dialog :title="title" v-model="open" width="1000px" append-to-body>
			<el-form ref="actActivityInfoRef" :model="form" :rules="rules" label-width="80px" :disabled="!isInsert && !isEdit">
				<el-row>
					<el-col :span="16">

						<el-row>
							<el-col :span="24">
								<el-form-item label="活动主题" prop="subject">
									<el-input v-model="form.subject" placeholder="请输入"/>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="参与人数" prop="participantsNumber">
									<el-input-number v-model="form.participantsNumber" placeholder="请输入"/>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="归属部门" prop="categoryId">
									<el-tree-select v-model="form.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id" placeholder="请选择归属部门" check-strictly default-expand-all filterable :disabled="!checkPermi(['admin'])"/>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="开始日期" prop="startDate">
									<el-date-picker clearable v-model="form.startDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择"/>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="结束日期" prop="endDate">
									<el-date-picker clearable v-model="form.endDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择"/>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="开始时间" prop="startTime">
									<el-time-select clearable v-model="form.startTime" start="06:00" step="00:10" end="23:00" placeholder="请选择"/>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="结束时间" prop="endTime">
									<el-time-select clearable v-model="form.endTime" start="06:00" step="00:10" end="23:00" placeholder="请选择"/>
								</el-form-item>
							</el-col>
							<el-col :span="24">
								<el-form-item label="活动地点" prop="activityAddr">
									<el-input v-model="form.activityAddr" placeholder="请输入"/>
								</el-form-item>
							</el-col>
						</el-row>
					</el-col>
					<el-col :span="8">
						<el-form-item label="宣传图" prop="image">
							<image-upload v-model="form.image"/>
						</el-form-item>
					</el-col>
					<el-col :span="24">

						<el-form-item label="活动详情" prop="details">
							<editor v-if="open" :min-height="292" v-model="form.details"/>
						</el-form-item>
					</el-col>


					<el-col :span="12">
						<el-form-item label="状态" prop="status">
							<el-radio-group v-model="form.status">
								<el-radio v-for="dict in sys_data_status" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="form.remark" type="textarea" placeholder="请输入"/>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer v-if="isEdit || isInsert">
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="ActActivityInfo">
import {
	listActActivityInfo,
	getActActivityInfo,
	delActActivityInfo,
	addActActivityInfo,
	updateActActivityInfo
} from "@/api/act/actActivityInfo";
import {checkPermi} from "@/utils/permission.js";
import useDeptStore from "@/store/modules/dept.js";

const {proxy} = getCurrentInstance();
const {sys_data_status} = proxy.useDict('sys_data_status');

const deptStore = useDeptStore();

const actActivityInfoList = ref([]);
const isEdit = ref(false);
const isInsert = ref(false);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
// 列显隐信息(able表示列是否可用，visible用于用户控制显隐)
const columns = ref([
	{label: `id`, able: false, visible: false, key: 'id'},
	{label: `活动主题`, able: true, visible: true, key: 'subject'},
	{label: `活动详情`, able: false, visible: true, key: 'details'},
	{label: `宣传图`, able: true, visible: true, key: 'image'},
	{label: `活动开始日期`, able: true, visible: true, key: 'startDate'},
	{label: `活动结束日期`, able: true, visible: true, key: 'endDate'},
	{label: `活动开始时间`, able: true, visible: true, key: 'startTime'},
	{label: `活动结束时间`, able: true, visible: true, key: 'endTime'},
	{label: `参与人数`, able: true, visible: true, key: 'participantsNumber'},
	{label: `活动地点`, able: true, visible: true, key: 'activityAddr'},
	{label: `状态`, able: true, visible: true, key: 'status'},
	{label: `部门id`, able: true, visible: true, key: 'deptId'},
	{label: `创建者`, able: false, visible: false, key: 'createBy'},
	{label: `创建时间`, able: false, visible: false, key: 'createTime'},
	{label: `更新者`, able: false, visible: false, key: 'updateBy'},
	{label: `更新时间`, able: false, visible: false, key: 'updateTime'},
	{label: `备注`, able: true, visible: false, key: 'remark'},
]);

const data = reactive({
	form: {},
	queryParams: {
		pageNum: 1,
		pageSize: 10,
		params: {},
		subject: null,
		details: null,
		startDate: null,
		endDate: null,
		participantsNumber: null,
		activityAddr: null,
		status: null,
		deptId: null,
	},
	rules: {
		subject: [{required: true, message: "活动主题不能为空", trigger: "blur"}],
		details: [{required: true, message: "活动详情不能为空", trigger: "blur"}],
		image: [{required: true, message: "宣传图不能为空", trigger: "blur"}],
		startDate: [{required: true, message: "活动开始日期不能为空", trigger: "blur"}],
		endDate: [{required: true, message: "活动结束日期不能为空", trigger: "blur"}],
		startTime: [{required: true, message: "活动开始时间不能为空", trigger: "blur"}],
		endTime: [{required: true, message: "活动结束时间不能为空", trigger: "blur"}],
		participantsNumber: [{required: true, message: "参与人数不能为空", trigger: "blur"}],
		activityAddr: [{required: true, message: "活动地点不能为空", trigger: "blur"}],
	}
});

const {queryParams, form, rules} = toRefs(data);

/** 列显隐 */
function columnVisible(key) {
	return proxy.checkColumn(columns.value, key);
}

/** 查询活动管理列表 */
function getList() {
	loading.value = true;
	listActActivityInfo(queryParams.value).then(response => {
		actActivityInfoList.value = response.rows;
		total.value = response.total;
		loading.value = false;
	});
}

// 取消按钮
function cancel() {
	open.value = false;
	reset();
}

// 表单重置
function reset() {
	form.value = {
		id: null,
		subject: null,
		details: null,
		image: null,
		startDate: null,
		endDate: null,
		startTime: null,
		endTime: null,
		participantsNumber: null,
		activityAddr: null,
		status: 0,
		deptId: null,
		createBy: null,
		createTime: null,
		updateBy: null,
		updateTime: null,
		remark: null
	};
	proxy.resetForm("actActivityInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
	queryParams.value.pageNum = 1;
	getList();
}

/** 重置按钮操作 */
function resetQuery() {
	proxy.resetForm("queryRef");
	handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
	ids.value = selection.map(item => item.id);
	single.value = selection.length != 1;
	multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
	reset();
	open.value = true;
	title.value = "添加活动管理";
	isInsert.value = true;
	isEdit.value = false;
}

/** 查看按钮操作 */
function handleView(row) {
	isEdit.value = false;
	title.value = "查看活动管理";
	getInfo(row);
}

/** 修改按钮操作 */
function handleUpdate(row) {
	isEdit.value = true;
	title.value = "修改活动管理";
	getInfo(row);
}

function getInfo(row) {
	reset();
	const _id = row.id || ids.value
	isInsert.value = false;
	proxy.modal.loading("加载中");
	getActActivityInfo(_id).then(response => {
		form.value = response.data;
		open.value = true;
		proxy.modal.closeLoading();
	}).catch(err => {
		Promise.reject(err);
		proxy.modal.closeLoading();
	});
}

/** 提交按钮 */
const actActivityInfoRef = ref(null)

function submitForm() {
	actActivityInfoRef.value.validate(valid => {
		if (valid) {
			if (form.value.id != null) {
				proxy.modal.loading("提交中");
				updateActActivityInfo(form.value).then(response => {
					proxy.modal.msgSuccess("修改成功");
					open.value = false;
					getList();
					proxy.modal.closeLoading();
				}).catch(err => {
					proxy.modal.closeLoading();
					Promise.reject(err);
				});
			} else {
				proxy.modal.loading("提交中");
				addActActivityInfo(form.value).then(response => {
					proxy.modal.msgSuccess("新增成功");
					open.value = false;
					getList();
					proxy.modal.closeLoading();
				}).catch(err => {
					proxy.modal.closeLoading();
					Promise.reject(err);
				});
			}
		}
	});
}

/** 删除按钮操作 */
function handleDelete(row) {
	const _ids = row.id || ids.value;
	proxy.modal.confirm('是否确认删除活动管理编号为"' + _ids + '"的数据项？').then(function () {
		proxy.modal.loading("删除中");
		return delActActivityInfo(_ids);
	}).then(() => {
		getList();
		proxy.modal.msgSuccess("删除成功");
		proxy.modal.closeLoading();
	}).catch(() => {
		proxy.modal.closeLoading();
	});
}

/** 导出按钮操作 */
function handleExport() {
	proxy.download.post('act/actActivityInfo/export', {
		...queryParams.value
	}, `actActivityInfo_${new Date().getTime()}.xlsx`)
}

/** 状态修改  */
function handleStatusChange(row, key) {
	let text = row[key] ? "停用" : "启用";
	proxy.modal.confirm('确认要"' + text + '】"' + row.id + '"数据项吗?').then(function () {
		return updateActActivityInfo({id: row.id, [key]: row[key]});
	}).then(() => {
		proxy.modal.msgSuccess(text + "成功");
	}).catch(function () {
		row[key] = row[key] ? 0 : 1;
	});
};

/** 查询部门下拉树结构 */
const deptOptions = ref(undefined);
watchEffect(() => {
	deptOptions.value = deptStore.deptTree;
});

getList();
</script>
